<template>
  <div class='xtx-carousel' @mouseenter="stop" @mouseleave="start">
    <!-- 大图 -->
    <ul class="carousel-body">
      <li :class="{fade:currentIndex===index}" v-for="item,index in swipers" :key="item.id" class="carousel-item">
        <RouterLink to="/">
          <img :src="item.imgUrl" alt="">
        </RouterLink>
      </li>
    </ul>
    <!-- 左右两侧箭头 -->
    <a @click="toggle(-1)" href="javascript:;" class="carousel-btn prev"><i class="iconfont icon-angle-left"></i></a>
    <a @click="toggle(1)" href="javascript:;" class="carousel-btn next"><i class="iconfont icon-angle-right"></i></a>
    <!-- 指示器下方小圆点 -->
    <div class="carousel-indicator">
      <!-- v-for如果遍历数字，从一开始 -->
      <span @click="currentIndex=index-1" :class="{active:currentIndex===index-1}" v-for="index in swipers.length" :key="index"></span>
    </div>
  </div>
</template>

<script>
import { onBeforeUnmount, ref, watch } from 'vue-demi'
export default {
  name: 'XtxCarousel',
  props: {
    // 轮播图数据列表
    swipers: {
      type: Array,
      required: true
    },
    // 自动轮播时间间隔
    duration: {
      type: Number,
      default: 1
    },
    // 是否自动轮播
    autoplay: {
      type: Boolean,
      default: true
    }
  },
  setup (props) {
    // 定义当前选中图片的索引
    const currentIndex = ref(0)
    // 自动轮播
    let timer = null
    const autoplayFn = () => {
      timer = setInterval(() => {
        currentIndex.value++
        // 超出数据长度
        if (currentIndex.value >= props.swipers.length) {
          currentIndex.value = 0
        }
      }, props.duration * 1000)
    }
    // 侦听器，侦听轮播图数据变化
    watch(() => props.swipers, () => {
      if (props.autoplay && props.swipers.length > 1) {
        autoplayFn()
      }
    }, { immediate: true })
    // 组件销毁时，清除定时器
    onBeforeUnmount(() => {
      if (timer) clearInterval(timer)
    })
    // 鼠标进入离开暂停和开始轮播
    // 重启轮播
    const start = () => {
      // 保证有且仅有一个定时器
      if (timer) clearInterval(timer)
      if (props.autoplay && props.swipers.length > 1) {
        autoplayFn()
      }
    }
    // 停止轮播
    const stop = () => {
      if (timer) clearInterval(timer)
    }
    // 点击切换
    const toggle = (n) => {
      if (n > 0) {
        // 向右
        currentIndex.value++
        if (currentIndex.value >= props.swipers.length) {
          currentIndex.value = 0
        }
      } else {
        // 向左
        currentIndex.value--
        if (currentIndex.value < 0) {
          currentIndex.value = props.swipers.length - 1
        }
      }
    }
    return {
      currentIndex,
      start,
      stop,
      toggle
    }
  }
}
</script>
<style scoped lang="less">
.xtx-carousel{
  width: 100%;
  height: 100%;
  min-width: 300px;
  min-height: 150px;
  position: relative;
  .carousel{
    &-body {
      width: 100%;
      height: 100%;
    }
    &-item {
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
      opacity: 0;
      transition: opacity 0.5s linear;
      &.fade {
        opacity: 1;
        z-index: 1;
      }
      img {
        width: 100%;
        height: 100%;
      }
    }
    &-indicator {
      position: absolute;
      left: 0;
      bottom: 20px;
      z-index: 2;
      width: 100%;
      text-align: center;
      span {
        display: inline-block;
        width: 12px;
        height: 12px;
        background: rgba(0,0,0,0.2);
        border-radius: 50%;
        cursor: pointer;
        ~ span {
          margin-left: 12px;
        }
        &.active {
          background:  #fff;
        }
      }
    }
    &-btn {
      width: 44px;
      height: 44px;
      background: rgba(0,0,0,.2);
      color: #fff;
      border-radius: 50%;
      position: absolute;
      top: 228px;
      z-index: 2;
      text-align: center;
      line-height: 44px;
      opacity: 0;
      transition: all 0.5s;
      &.prev{
        left: 20px;
      }
      &.next{
        right: 20px;
      }
    }
  }
  &:hover {
    .carousel-btn {
      opacity: 1;
    }
  }
}
</style>
